<template>
  <div class="app-container">
    <el-form size="mini" :inline="true" :model="searchForm" @keyup.enter.native="getDataList()">
      <el-form-item label="查询：">
        <el-input clearable placeholder="可输入用户名称/手机号/账号" style="width: 300px" v-model="searchForm.name"></el-input>
      </el-form-item>
      <el-form-item label="创建时间：">
        <el-date-picker end-placeholder="结束日期" range-separator="至" start-placeholder="开始日期" type="daterange" v-model="searchForm.date" value-format="yyyy-MM-dd"> </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button size="mini" @click="getDataList()" icon="el-icon-search" type="primary">查询</el-button>
        <el-button size="mini" @click="refDataList()" icon="el-icon-refresh" type="primary">重置</el-button>
        <el-button size="mini" @click="addOrUpdateHandle()" type="primary" v-if="Jurisdictionlist.indexOf('Add') > -1">新增用户</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" size="mini" @selection-change="selectionChangeHandle" border style="width: 100%" v-loading="dataListLoading">
      <el-table-column align="center" header-align="center" label="编号" prop="typeName" width="80">
        <template slot-scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" header-align="center" label="企业微信id" prop="name"></el-table-column>
      <el-table-column align="center" header-align="center" label="姓名" prop="nickname"></el-table-column>
      <el-table-column align="center" header-align="center" label="登录账号" prop="account"></el-table-column>
      <el-table-column align="center" header-align="center" label="手机号码" prop="phone"></el-table-column>
      <el-table-column align="center" header-align="center" label="所属角色" prop="rolesId">
        <template slot-scope="scope">{{ scope.row.rolesId | levelDicFilter }} </template>
      </el-table-column>
      <el-table-column align="center" header-align="center" label="创建时间" prop="created_time" width="200"></el-table-column>
      <el-table-column align="center" fixed="right" header-align="center" label="操作" width="200">
        <template slot-scope="scope">
          <el-button size="mini" @click="addOrUpdateHandle(scope.row)" plain type="primary" v-if="Jurisdictionlist.indexOf('Edit') > -1">编辑</el-button>
          <el-button size="mini" @click="handleDel(scope.row)" plain type="danger" v-if="Jurisdictionlist.indexOf('Delete') > -1">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination small :current-page="pageIndex" :page-size="pageSize" :page-sizes="[10, 20, 50, 100]" :total="totalPage" @current-change="currentChangeHandle" @size-change="sizeChangeHandle" layout="total, sizes, prev, pager, next, jumper" style="margin-top: 10px"></el-pagination>

    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update @refreshList="getDataList" ref="addOrUpdate"></add-or-update>
    <!-- 删除前通过发送验证码检验身份，此为验证码弹框 -->
  </div>
</template>
<script>
import AddOrUpdate from './add-or-update'
import city from './city.json'
import { usersApi, delUserApi } from '@/api/system/index.js'
import { roleAllApi } from '../../../api/commonApi'
var app
export default {
  beforeCreate() {
    app = this
  },
  filters: {
    levelDicFilter(value) {
      var return_name
      app.allRoleArr.forEach(function (element) {
        if (element.id == value) {
          return_name = element.name
        }
      })
      return return_name
    },
  },
  data() {
    return {
      allRoleArr: [],
      delrow: [], // 删除行信息
      dialogFormVisible: false, // 修改密码弹框控制
      timer: undefined, // 倒计时全局删除
      isNext: false,
      time: 90, // 发送短信时间,倒计时90s
      rules: {
        code: [{ required: true, message: '请输入验证码', trigger: 'blur' }],
      },
      dataForm: {
        code: undefined,
      },
      Jurisdictionlist: [], // 权限列表
      searchForm: {
        name: '',
        date: [],
      },
      cityOptions: city,
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      roleOptions: [],
    }
  },
  components: { AddOrUpdate },
  mounted() {
    this.getAllRoleArr()
    this.getDataList()
    // 处理权限
    this.Jurisdictionlist = JSON.parse(localStorage.getItem('jurisdiction'))[this.$route.path] || []
  },
  computed: {},
  methods: {
    getAllRoleArr() {
      roleAllApi({}).then((res) => {
        if (res.code == 200) {
          this.allRoleArr = res.result
        }
      })
    },
    qxyzm() {
      this.dialogFormVisible = false
      this.time = 90
      this.isNext = false
      clearInterval(this.timer)
      this.$refs.dataForm.resetFields()
    },
    getcode() {
      this.sendYanzheng()
    },
    finaldel(row) {
      this.$confirm('确认删除吗?', '提示', {
        type: 'warning',
      })
        .then(() => {
          let para = {
            ids: [row.id],
            code: this.dataForm.code,
          }
          delUserApi(para).then((res) => {
            if (res.code == 200) {
              this.$message.success('删除成功')
              this.getDataList()
            }
          })
        })
        .catch(() => {})
    },
    modifypass() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          var that = this
          let para = {
            id: this.delrow.id,
          }
          delUserApi(para).then((res) => {
            if (res.code == 200) {
              this.$message.success('删除成功')
              this.qxyzm()
              this.getDataList()
            }
          })
        }
      })
    },
    //重置
    refDataList() {
      this.searchForm = {
        name: '',
        organizationId: [],
        date: [],
        if_show: '',
      }
      this.getDataList()
    },
    // handleDel(row) {
    //   this.dialogFormVisible = true
    //   this.delrow = row
    // },
    handleDel(row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning',
      })
        .then(() => {
          let para = { id: row.id }
          delUserApi(para).then((res) => {
            if (res.code == 200) {
              this.$message.success('删除成功')
              this.getDataList()
            }
          })
        })
        .catch(() => {})
    },

    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      const info = Object.assign({ pageNumber: this.pageIndex, pageSize: this.pageSize }, this.searchForm)
      usersApi(info)
        .then((res) => {
          this.dataList = res.result.items
          this.totalPage = res.result.total
          this.dataListLoading = false
        })
        .catch((e) => {
          this.dataList = []
          this.totalPage = 0
          this.dataListLoading = false
        })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {},
    // 新增 / 修改
    addOrUpdateHandle(row) {
      this.$refs.addOrUpdate.open(row, this.allRoleArr)
    },
  },
}
</script>

<style lang="scss" scoped>
.my-btn .el-form-item__content {
  width: 100%;
}
.getCode {
  width: 100px;
  height: 40px;
  line-height: 40px;
  text-align: center;
  display: inline-block;
  color: #fff;
  background-color: #409eff;
  cursor: pointer;
  box-sizing: border-box;
  font-size: 14px;
  border-radius: 4px;
  margin-left: 20px;
}
</style>
